package main

import (
	"context"
	"feishup2pclient/api"
	"feishup2pclient/p2p"
	"feishup2pclient/socket"
	"fmt"
)

var (
	ctx      context.Context
	stopFunc context.CancelFunc // 全局变量通道
)

type TunStatus struct {
	ctx context.Context
}

func NewTunStatus() *TunStatus {
	return &TunStatus{}
}

func (a *TunStatus) startup(ctx context.Context) {
	a.ctx = ctx
}

func (a *TunStatus) Get_tun() string {
	//  if p2p.Dev.LUID() == 0 {
	// 	return "err"
	//  }
	// 判断 Wintun 接口是否已经打开
	//if p2p.Dev.File() == nil {
	if !socket.SocketRun {
		//log.Fatal("关闭状态")
		return "err"
	} else {
		return "ok"
	}

}

// 网卡关闭，需要关闭所有线程
func (a *TunStatus) Tun_close() {
	fmt.Printf("tun is closed")
	stopFunc()
	if err := p2p.Dev.Close(); err != nil {
		fmt.Println(err)
	}

	//socket.SocketRun = false
	socket.Socket_exit = true
	//p2p.P2P_exit = true
	// 向带缓冲通道发送关闭信号

}

// 返回list 信息
func (a *TunStatus) GetStatus() []map[string]string {
	return api.GetStatus()
}
